const db = require("../db/index");

exports.addOneComment = (req, res) => {
    const comment = req.body
    // console.log(comment)
    const sql = "insert into comments set ?"
    db.query(sql,
        { articleId: comment.articleId, userId: comment.userId, content: comment.content },
        (err, result) => {
            if (err) return res.show(err);
            if (result.affectedRows !== 1) return res.show("新增评论失败！");

            const sql = "update articles set comment = comment + 1 where articleId = ?";
            db.query(sql, comment.articleId, (err, result) => {
                if (err) return res.show(err);
                if (result.affectedRows !== 1)
                  return res.show("修改文章评论数失败！");
                res.show("新增评论成功！", 0);
            })
        })
}
